package com.xuecheng.learning.service.impl;

import com.fasterxml.jackson.core.JsonParser;
import com.google.gson.JsonObject;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.base.utils.JsonUtil;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.learning.feignclient.ContentServiceClient;
import com.xuecheng.learning.feignclient.MediaServiceClient;
import com.xuecheng.learning.model.dto.XcCourseTablesDto;
import com.xuecheng.learning.service.LearningService;
import com.xuecheng.learning.service.MyCourseTablesService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author shiminghui
 * @date 2024/9/20 20:57
 * @description: TODO
 */
public class LearningServiceImpl implements LearningService {

    @Autowired
    private ContentServiceClient contentServiceClient;
    @Autowired
    private MyCourseTablesService myCourseTablesService;
    @Autowired
    private MediaServiceClient mediaServiceClient;

    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {

        //首先查询课程是否存在(远程调用课程管理服务)
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if (coursepublish == null) {
            return RestResponse.validfail("课程不存在");
        }

        //:判断课程是否可以试看(相当于免费),查询课程计划,判断这一节课是否可以试看
        String teachplanJson = coursepublish.getTeachplan();
        List<Teachplan> teachplans = JsonUtil.jsonToList(teachplanJson, Teachplan.class);
        Teachplan teachplan = new Teachplan();
        for (Teachplan teachplan1 : teachplans) {
            if (teachplan1.getId().equals(teachplanId)) {
                teachplan = teachplan1;
                break;
            }
        }
        String isPreview = teachplan.getIsPreview();
        if (isPreview == "1") {
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }


        //判断是否登录
        if (!userId.isEmpty()) {

            //获取学习资格
            XcCourseTablesDto leanringStatus = myCourseTablesService.getLeanringStatus(userId, courseId);
            //学习资格，[{"code":"702001","desc":"正常学习"},{"code":"702002","desc":"没有选课或选课后没有支付"},{"code":"702003","desc":"已过期需要申请续期或重新支付"}]
            String learnStatus = leanringStatus.getLearnStatus();
            if (learnStatus.equals("702001")) {
                return mediaServiceClient.getPlayUrlByMediaId(mediaId);
            } else if (learnStatus.equals("702002")) {
                return RestResponse.validfail("无法观看，由于没有选课或选课后没有支付");
            } else if (learnStatus.equals("702003")) {
                return RestResponse.validfail("您的选课已过期需要申请续期或重新支付");
            }
        }

        //没有登录,则判断课程是否免费
        String charge = coursepublish.getCharge();
        if (charge.equals("201000")) {//免费可以正常学习
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }

        return RestResponse.validfail("请购买课程后继续学习");
    }
}
